//
//  WYMainController.m
//  WYNews
//
//  Created by 曾佳豪 on 2017/3/18.
//  Copyright © 2017年 haodada.com. All rights reserved.
//

#import "WYMainController.h"

@interface WYMainController ()

@end

@implementation WYMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view .backgroundColor =[UIColor orangeColor];
    [self addChildControllers];
}
-(void)addChildControllers{
    //添加所有子控制器
    //每个控制器都有3个不同的参数 类名clsName 标题title 图片icon
    NSArray *child =@[
                      @{@"clsName":@"WYHomeController",@"title":@"首页",@"icon":@"news"},
                      @{@"clsName":@"UIViewController",@"title":@"阅读",@"icon":@"reader"},
                      @{@"clsName":@"UIViewController",@"title":@"视频",@"icon":@"media"},
  @{@"clsName":@"UIViewController",@"title":@"话题",@"icon":@"found"},
  @{@"clsName":@"UIViewController",@"title":@"我",@"icon":@"me"}];
    
    //创建可变数组
    NSMutableArray *children =[NSMutableArray array];
    
    //循环创建各个子控制器
    for (NSDictionary *dic  in child ) {
        UINavigationController *child =[self childController:dic];
        [children addObject:child];
    }
    self.viewControllers =children;
    
    
}
//封装创建单个 子控制器
//子控制器的字典参数
-(UINavigationController *)childController:(NSDictionary *)dic{
    //1.拿到类名
    NSString *clsName =dic[@"clsName"];
    //2.生成子控制器
    UIViewController *controller =[NSClassFromString(clsName)new];
    //3.设置子控制器的title
    controller.title =dic[@"title"];
    
   //4.设置文字选中状态的颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    //5.设置子控制器的正常图片
    // imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
    //设置为原生颜色
    UIImage *image =[[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_normal",dic[@"icon"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //5.1设置正常图片
    controller.tabBarItem.image =image;
    
    //6.设置子控制器的选中图片
    UIImage *selectedImage =[[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_highlight",dic[@"icon"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //6.1设置选中图片
    controller.tabBarItem.selectedImage =selectedImage;
    
    //7.把子控制器设置为导航控制器的根控制器
    //创建导航控制器 设置root
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:controller];
    //8.返回控制器
    return nav;
    
    
}

@end
